package com.reins.bookstore.controller;

import com.reins.bookstore.entity.UserAuth;
import com.reins.bookstore.object.Filter;
import com.reins.bookstore.object.UserDTO;
import com.reins.bookstore.object.UserRate;
import com.reins.bookstore.service.BasketService;
import com.reins.bookstore.service.OrderService;
import com.reins.bookstore.service.UserService;
import com.reins.bookstore.utils.sortUtils.sortUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private BasketService basketService;

    @Autowired
    private OrderService orderService;

    @RequestMapping("/checkUser")
    public UserAuth checkUser(@RequestParam("username") String username,@RequestParam("password") String password){
        return userService.checkUser(username, password);
    }

    @RequestMapping("/editUserwithpassword")
    public UserDTO editUser(@RequestBody UserDTO params, @RequestParam("password") String password){
        return userService.editUser(params,password);
    }

    @RequestMapping("/editUser")
    public UserDTO editUser(@RequestBody UserDTO params){
        return userService.editUser(params);
    }

    @RequestMapping("/changeBanned")
    public UserAuth changeBanned(@RequestParam("userId")Integer id)
    {
        return userService.changeBanned(id);
    }

    @RequestMapping("/changeType")
    public UserAuth changeType(@RequestParam("userId")Integer id)
    {
        return userService.changeType(id);
    }

    @RequestMapping("/findAllUsers")
    public List<UserDTO> findAllUsers(){
        return userService.findAllUsers();
    }


    @RequestMapping("/getAllUserAuthSortedByType")
    public List<UserAuth> getAllUserAuthSortedByType(@RequestBody Map<String, Integer> params){
        Integer startId = params.get("startId");
        Integer endId = params.get("endId");
        List<UserAuth> userAuthList = userService.findUserAuthInRange(startId, endId);
        return sortUtils.sortUserAuthByType(userAuthList);
    }


    @RequestMapping("/register")
    public UserAuth createUser(@RequestBody UserDTO params, @RequestParam("password") String password){
        return userService.createUser(params,password);
    }

    @RequestMapping("/deleteUser")
    public Integer deleteUser(@RequestBody UserDTO params){
        Integer res;
        try{
            basketService.deleteAllByUserId(params.getUserId());
            orderService.deleteOrderByUserId(params.getUserId());
            res = userService.deleteUser(params.getUserId());
        }catch (Exception ex){
            return 0;
        }
        return res;
    }

    @RequestMapping("/getUserRate")
    public List<UserRate> getUserRate(@RequestBody Filter params){
        if (params.getPages() == null)
            params.setPages(1);
        if (params.getUserId() == null)
            params.setUserId(0);
        return userService.getUserRate(params);
    }

    @RequestMapping("/getAllUserRate")
    public List<UserRate> getAllUserRate(){
        return userService.getUserRate(null);
    }

}
